package com.glory.good_dark.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.BatchWorkBeginListAdapter;
import com.glory.good_dark.adapter.MaterielListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.EqpStateEntity;
import com.glory.good_dark.entity.LotInfoEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import org.json.JSONObject;

import java.util.List;
import java.util.function.Consumer;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author yi.zhang
 * @date 2019/6/13
 * @desc 批次开始作业
 */
public class BatchWorkBeginActivity extends BaseProjectActivity {

    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.lot_Txt)
    TextView lotTxt;
    @BindView(R.id.lot_Ly)
    LinearLayout lotLy;
    @BindView(R.id.tvEqpStatus)
    TextView tvEqpStatus;
    @BindView(R.id.elvLotInfo)
    ExpandableListView elvLotInfo;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private FreeUI_GeneralFragmentDialog dialog;
    private int PROCESS_CRR = 0;
    private List<LotInfoEntity> lotList;
    private BatchWorkBeginListAdapter batchWorkBeginListAdapter;

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {
        if (tag == PROCESS_CRR) {
//            tvProcess.setText(content);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bondingworkbegin;
    }

    @Override
    protected void initView() {
        EQPIDTxt.setText(App.EQPID);
//        initRecyclerView(materielRv, this);
//        materielListAdapter = new MaterielListAdapter(null, false);
//        materielListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//        materielListAdapter.isFirstOnly(true);
//        materielRv.setAdapter(materielListAdapter);
        batchWorkBeginListAdapter = new BatchWorkBeginListAdapter(this,getSupportFragmentManager());
        elvLotInfo.setAdapter(batchWorkBeginListAdapter);

    }

    @Override
    protected void initData() {
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDAGetEqpLotBomInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"BODY\":{\n" +
//                "            \"EQP_ID\":\"LC128\",\n" +
//                "            \"EQP_RUN_STATE\":\"\",\n" +
//                "            \"EQP_RUN_STATE_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "            \"EQP_RUN_LOTLIST\":[\n" +
//                "                {\n" +
//                "                    \"LOT_ID\":\"TS00001\",\n" +
//                "                    \"EQP_ID\":\"LC128\",\n" +
//                "                    \"EQP_NAME\":\"AD125\",\n" +
//                "                    \"EQP_TYPE\":\"TS00001\",\n" +
//                "                    \"EQP_RUN_STATE\":\"运行中\",\n" +
//                "                    \"EQP_RUN_STATE_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"PART_NAME\":\"产品名称\",\n" +
//                "                    \"INPUT_QTY\":2100,\n" +
//                "                    \"START_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"END_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"RECIPE_NAME\":\"AD125\",\n" +
//                "                    \"ID\":333,\n" +
//                "                    \"RUN_ID\":102,\n" +
//                "                    \"PROCESS\":\"GX001\",\n" +
//                "                    \"REMARK\":\"\",\n" +
//                "                    \"TRACKIN_PROCESS_CRR\":\"0\",\n" +
//                "                    \"TRACKIN_CRR_ID\":\"TS00001\",\n" +
//                "                    \"TRACKOUT_PROCESS_CRR\":\"0\",\n" +
//                "                    \"TRACKOUT_CRR_ID\":\"TS00001\",\n" +
//                "                    \"MES_MAT_LIST\":[\n" +
//                "                        {\n" +
//                "                            \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                            \"MATERIAL_ID\":\"T1\",\n" +
//                "                            \"MATERIAL_TYPE\":\"4KUAISU1\",\n" +
//                "                            \"MATERIAL_NAME\":\"晶粒\"\n" +
//                "                        },\n" +
//                "                        {\n" +
//                "                            \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                            \"MATERIAL_ID\":\"T2\",\n" +
//                "                            \"MATERIAL_TYPE\":\"4KUAISU2\",\n" +
//                "                            \"MATERIAL_NAME\":\"焊条\"\n" +
//                "                        },\n" +
//                "                        {\n" +
//                "                            \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                            \"MATERIAL_ID\":\"T3\",\n" +
//                "                            \"MATERIAL_TYPE\":\"4KUAISU3\",\n" +
//                "                            \"MATERIAL_NAME\":\"环氧\"\n" +
//                "                        }\n" +
//                "                    ]\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"LOT_ID\":\"TS00002\",\n" +
//                "                    \"EQP_ID\":\"LC128\",\n" +
//                "                    \"EQP_NAME\":\"AD125\",\n" +
//                "                    \"EQP_TYPE\":\"TS00001\",\n" +
//                "                    \"EQP_RUN_STATE\":\"运行中\",\n" +
//                "                    \"EQP_RUN_STATE_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"PART_NAME\":\"产品名称\",\n" +
//                "                    \"INPUT_QTY\":2100,\n" +
//                "                    \"START_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"END_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"RECIPE_NAME\":\"AD125\",\n" +
//                "                    \"ID\":333,\n" +
//                "                    \"RUN_ID\":102,\n" +
//                "                    \"PROCESS\":\"GX001\",\n" +
//                "                    \"REMARK\":\"   \",\n" +
//                "                    \"TRACKIN_PROCESS_CRR\":\"0\",\n" +
//                "                    \"TRACKIN_CRR_ID\":\"TS00001\",\n" +
//                "                    \"TRACKOUT_PROCESS_CRR\":\"0\",\n" +
//                "                    \"TRACKOUT_CRR_ID\":\"TS00001\",\n" +
//                "                    \"MES_MAT_LIST\":[\n" +
//                "                        {\n" +
//                "                            \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                            \"MATERIAL_ID\":\"T1\",\n" +
//                "                            \"MATERIAL_TYPE\":\"4KUAISU1\",\n" +
//                "                            \"MATERIAL_NAME\":\"晶粒\"\n" +
//                "                        },\n" +
//                "                        {\n" +
//                "                            \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                            \"MATERIAL_ID\":\"T2\",\n" +
//                "                            \"MATERIAL_TYPE\":\"4KUAISU2\",\n" +
//                "                            \"MATERIAL_NAME\":\"焊条\"\n" +
//                "                        },\n" +
//                "                        {\n" +
//                "                            \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                            \"MATERIAL_ID\":\"T3\",\n" +
//                "                            \"MATERIAL_TYPE\":\"4KUAISU3\",\n" +
//                "                            \"MATERIAL_NAME\":\"环氧\"\n" +
//                "                        }\n" +
//                "                    ]\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"RETURN\":{\n" +
//                "            \"RETURNCODE\":\"0\",\n" +
//                "            \"RETURNMESSAGE\":\"\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//        BaseEntity<EqpStateEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<EqpStateEntity>>() {}.getType());
//        if (bean.isSuccess(this,getSupportFragmentManager())){
//            lotList = bean.getData().getBody().getEQP_RUN_LOTLIST();
//            batchWorkBeginListAdapter.setData(lotList);
//        }
        FreeUI_EntityCallBack<BaseEntity<EqpStateEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<EqpStateEntity>>
                (new TypeToken<BaseEntity<EqpStateEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<EqpStateEntity>> response) {
                if (response.body().isSuccess(BatchWorkBeginActivity.this, getSupportFragmentManager(), true)) {
                    tvEqpStatus.setText(response.body().getData().getBody().getEQP_RUN_STATE());
//                    lotList = response.body().getData().getBody().getEQP_RUN_LOTLIST();
//                    if (lotList.size() <= 0) {
//                        return;
//                    }
//
//                    batchWorkBeginListAdapter.setData(lotList);

                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<EqpStateEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAGetEqpLotBomInfo");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAGetEqpLotBomInfo", jsonObject);

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //重新回到当前页面不需要刷新数据
//        getLotInfo(lotTxt.getText().toString());
    }

    private void fillLotData(LotInfoEntity lotInfoEntity) {
//        lotTxt.setText(lotInfoEntity.getLOT_ID());
//        productTxt.setText(lotInfoEntity.getPART_NAME());
//        numberTxt.setText(String.valueOf(lotInfoEntity.getINPUT_QTY()));
//        process.setText(lotInfoEntity.getPROCESS());
//        tvProcess.setText(lotInfoEntity.getTRACKIN_CRR_ID());
//        materielListAdapter.setNewData(lotInfoEntity.getMES_MAT_LIST());
        batchWorkBeginListAdapter.addData(lotInfoEntity);


    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("批次开始操作");
        freeUI_titleView.setRightListener("开始作业", v -> {
            LotInfoEntity lotInfoEntity = batchWorkBeginListAdapter.getGroup(lotList.size());
            if (lotInfoEntity.getTRACKIN_PROCESS_CRR().equals("1") || lotInfoEntity.getTRACKOUT_PROCESS_CRR().equals("1")) {
                if (lotInfoEntity.getPROCESS().equals("")) {
                    showToast("请输入或扫描运输筐号", false);
                    return;
                }
            }
            if (lotTxt.getText().toString().equals("")) {
                showToast("请输入或扫描批次信息", false);
            } else if (EQPIDTxt.getText().toString().equals("")) {
                showToast("请输入或扫描设备信息", false);
                finish();
//            } else if (lotInfoEntity.getPROCESS().equals("Q040")) {
//                FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "是否调整轨道", new FreeUI_CommonCallback() {
//                    @Override
//                    public void onSuccess() {
//                        submit(lotInfoEntity.getLOT_ID(), 1,lotInfoEntity.getPROCESS());
//                    }
//                }, new FreeUI_CommonCallback() {
//                    @Override
//                    public void onSuccess() {
//                        submit(lotInfoEntity.getLOT_ID(), 0,lotInfoEntity.getPROCESS());
//                    }
//                }).show(getSupportFragmentManager(), "");
            } else {
                submit(lotInfoEntity.getLOT_ID(), 2,lotInfoEntity.getPROCESS());
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void getLotInfo(String lotID) {
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDAEQPInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"BODY\":{\n" +
//                "            \"LOT_ID\":\"TS00005\",\n" +
//                "            \"EQP_ID\":\"LC128\",\n" +
//                "            \"EQP_NAME\":\"AD125\",\n" +
//                "            \"EQP_TYPE\":\"TS00001\",\n" +
//                "            \"EQP_RUN_STATE\":\"运行中\",\n" +
//                "            \"EQP_RUN_STATE_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "            \"PART_NAME\":\"产品名称\",\n" +
//                "            \"INPUT_QTY\":2100,\n" +
//                "            \"START_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "            \"END_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "            \"RECIPE_NAME\":\"AD125\",\n" +
//                "            \"ID\":333,\n" +
//                "            \"RUN_ID\":102,\n" +
//                "            \"PROCESS\":\"GX001\",\n" +
//                "            \"REMARK\":\"   \",\n" +
//                "            \"TRACKIN_PROCESS_CRR\":\"0\",\n" +
//                "            \"TRACKIN_CRR_ID\":\"TS00001\",\n" +
//                "            \"TRACKOUT_PROCESS_CRR\":\"0\",\n" +
//                "            \"TRACKOUT_CRR_ID\":\"TS00001\",\n" +
//                "            \"MES_MAT_LIST\":[\n" +
//                "                {\n" +
//                "                    \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                    \"MATERIAL_ID\":\"T1\",\n" +
//                "                    \"MATERIAL_TYPE\":\"4KUAISU1\",\n" +
//                "                    \"MATERIAL_NAME\":\"晶粒\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                    \"MATERIAL_ID\":\"T2\",\n" +
//                "                    \"MATERIAL_TYPE\":\"4KUAISU2\",\n" +
//                "                    \"MATERIAL_NAME\":\"焊条\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"MES_RUN_HISTORY_ID\":333,\n" +
//                "                    \"MATERIAL_ID\":\"T3\",\n" +
//                "                    \"MATERIAL_TYPE\":\"4KUAISU3\",\n" +
//                "                    \"MATERIAL_NAME\":\"环氧\"\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"RETURN\":{\n" +
//                "            \"RETURNCODE\":\"0\",\n" +
//                "            \"RETURNMESSAGE\":\"\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//        BaseEntity<LotInfoEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<LotInfoEntity>>() {}.getType());
//        if (bean.isSuccess(this,getSupportFragmentManager())){
//            LotInfoEntity body = bean.getData().getBody();
//            fillLotData(body);
//        }
        FreeUI_EntityCallBack<BaseEntity<LotInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<LotInfoEntity>>
                (new TypeToken<BaseEntity<LotInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<LotInfoEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(), getSupportFragmentManager())) {
                    LotInfoEntity lotInfoEntity = response.body().getData().getBody();
                    fillLotData(lotInfoEntity);
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<LotInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAEQPInfo");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("LOT_ID", lotID);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAEQPInfo", jsonObject);
    }


    /**
     * 提交
     */
    private void submit(String lotNum, int adjustTrack,String process) {
//        showToast("开始作业成功",true);

        FreeUI_EntityCallBack<BaseEntity<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<JSONObject>>
                (new TypeToken<BaseEntity<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<JSONObject>> response) {
                if (response.body().isSuccess(getApplicationContext(), getSupportFragmentManager())) {
                    showToast("开始作业成功", true);
//                    showConfirmDialog(BatchWorkBeginActivity.this, "开始作业成功");
                    lotList.clear();
                    lotTxt.setText("");
                    initData();
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "TrankIn");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("LOT_ID", lotNum);
        jsonObject.addProperty("CRR_ID", process);
        jsonObject.addProperty("ADJUST_TRACK", adjustTrack);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDATrankIn", jsonObject);
    }

//    R.id.adjustable_But, R.id.replaceMateriel_But, R.id.process_Txt
    @OnClick({R.id.lot_Ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lot_Ly:
                if (App.EQPID.equals("")) {
                    showToast("设备信息为空", false);
                    return;
                }

                if (!lotTxt.getText().toString().equals("")){
                    return;
                }

                dialog = FreeApi_DialogUtils.getScanDialog(this, "请输入批号", s -> {
                    for (LotInfoEntity lotInfoEntity : lotList){
                        if (lotInfoEntity.getLOT_ID().equals(s)){
                            showToast("该批次已经在本设备上运行",false);
                            return;
                        }
                    }
                    lotTxt.setText(s);
                    getLotInfo(s);
                    dialog.dismiss();
                });
                dialog.show(getSupportFragmentManager(), "END_DIALOG");
                break;
//            case R.id.adjustable_But:
//                materielRv.setVisibility(materielRv.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
//                btnAdjustable.setText(materielRv.getVisibility() == View.VISIBLE ? "收起" : "展开");
//                break;
//            case R.id.replaceMateriel_But:
//                FreeApi_Utils.skipWithExtraIntent("header", BatchWorkBeginActivity.this, ReplaceMaterielActivity.class);
//                break;
//            case R.id.process_Txt:
//                showSpanDialog(PROCESS_CRR, "请输入或扫描运输筐号");
//                break;
        }
    }

}
